package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.entity.Collect;
import com.example.demo.entity.vo.CollectVo;
import com.example.demo.exception.bizException.BizException;
import com.example.demo.mapper.CollectMapper;
import com.example.demo.service.AchieveService;
import com.example.demo.service.CollectService;
import com.example.demo.service.DemandService;
import com.example.demo.service.PatentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* <p>
*  服务实现类
* </p>
*
* @author cy
* @since 2020-11-04
*/
@Slf4j
@Service
public class CollectServiceImpl extends ServiceImpl<CollectMapper, Collect> implements CollectService {

    @Autowired
    private PatentService patentService;

    @Autowired
    private DemandService demandService;

    @Autowired
    private AchieveService achieveService;


    @Override
    public Page<CollectVo> listCollectsByPage(int page, int pageSize, int userId) {
        log.info("正在执行分页查询collect: page = {} pageSize = {} userId = {}",page,pageSize,userId);
        QueryWrapper<Collect> queryWrapper =  new QueryWrapper<Collect>().eq("user_id", userId);
        Page<Collect> result = super.page(new Page<>(page, pageSize),queryWrapper);
        Page<CollectVo> voPage = new Page<>();
        List<CollectVo> collectVos = new ArrayList<>(result.getRecords().size());
        for (Collect collect:result.getRecords()) {
            CollectVo collectVo = this.getCollectVo(collect);
            collectVos.add(collectVo);
        }
        BeanUtils.copyProperties(result,voPage);
        voPage.setRecords(collectVos);
        log.info("分页查询collect完毕: 结果数 = {} ",result.getRecords().size());
        return voPage;
    }

    @Override
    public CollectVo getCollectById(int id) {
        log.info("正在查询collect中id为{}的数据",id);
        CollectVo collect = this.getCollectVo(super.getById(id));
        log.info("查询id为{}的collect{}",id,(null == collect?"无结果":"成功"));
        return collect;
    }

    @Override
    public int insertCollect(Collect collect) {
        log.info("正在插入collect");
        if (super.getOne(new QueryWrapper<Collect>().eq("foreign_id",collect.getForeignId()).eq("type",collect.getType()).eq("user_id",collect.getUserId())) != null){
            throw new BizException("此收藏已存在，请勿重复添加");
        }
        if (this.checkIsHave(collect)){
            if (super.save(collect)) {
                log.info("插入collect成功,id为{}",collect.getCollectId());
                return collect.getCollectId();
            } else {
                log.error("插入collect失败");
                throw new BizException("添加失败");
            }
        }
        return 0;
    }

    @Override
    public int deleteCollectById(int id) {
        log.info("正在删除id为{}的collect",id);
        if (super.removeById(id)) {
            log.info("删除id为{}的collect成功",id);
            return id;
        } else {
            log.error("删除id为{}的collect失败",id);
            throw new BizException("删除失败[id=" + id + "]");
        }
    }

    @Override
    public void deleteCollectByForeignId(int foreignId,int type) {
        super.remove(new QueryWrapper<Collect>().eq("foreign_id",foreignId).eq("type",type));
    }

    @Override
    public int updateCollect(Collect collect) {
        log.info("正在更新id为{}的collect",collect.getCollectId());
        if (super.updateById(collect)) {
            log.info("更新d为{}的collect成功",collect.getCollectId());
            return collect.getCollectId();
        } else {
            log.error("更新id为{}的collect失败",collect.getCollectId());
            throw new BizException("更新失败[id=" + collect.getCollectId() + "]");
        }
    }


    private boolean checkIsHave(Collect collect){
        switch (collect.getType()){
            case Collect.ACHIEVE_TYPE:
                if (achieveService.getJustAchieveById(collect.getForeignId())!=null){
                    return true;
                }else{
                    throw new BizException("没有此成果，请输入正确的成果id");
                }
            case Collect.PATENT_TYPE:
                if (patentService.getPatentById(collect.getForeignId())!=null){
                    return true;
                }else{
                    throw new BizException("没有此专利，请输入正确的专利id");
                }
            case Collect.DEMAND_TYPE:
                if (demandService.getJustDemandById(collect.getForeignId())!=null){
                    return true;
                }else{
                    throw new BizException("没有此需求，请输入正确的需求id");
                }
        }
        return false;
    }

    private CollectVo getCollectVo(Collect collect){
        CollectVo collectVo = new CollectVo();
        collectVo.setCollect(collect);
        if (collect.getType() == Collect.ACHIEVE_TYPE){
            collectVo.setObject(achieveService.getJustAchieveById(collect.getForeignId()));
        }else if (collect.getType() == Collect.PATENT_TYPE){
            collectVo.setObject(patentService.getPatentById(collect.getForeignId()));
        }else if (collect.getType() == Collect.DEMAND_TYPE){
            collectVo.setObject(demandService.getJustDemandById(collect.getForeignId()));
        }
        return collectVo;
    }
}
